<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>无标题文档</title>
    <link href="../../../static/css/style.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="../../../static/js/jquery.js"></script>
</head>

<body>
<div class="place"><span>位置：</span>
    <ul class="placeul">
        <li>数据统计</li>
        <li>厂商采购统计</li>
        <li>采购详情</li>
    </ul>
</div>
<div class="rightinfo">
    <form action="" method="post">
        <table class="tablelist">
            <thead>
            <tr>
                <th>序号</th>
                <th>厂商名称</th>
                <th>采购单编号</th>
                <th>商品数量</th>
                <th>金额</th>
                <th>采购时间</th>
                <th>操作人</th>
                <th>采购单详情</th>
            </tr>
            </thead>
            <tbody id="purchasetr">
            <tr>
                <td>1</td>
                <td>阿里巴巴</td>
                <td>CG201701270001</td>
                <td>5</td>
                <td>￥76,582</td>
                <td>2017-01-25 15:05:05</td>
                <td>马云</td>
                <td>
                    <a href="/purchase/purchase/purchaseView" class="tablelink">查看详情</a>
                </td>
            </tr>
            </tbody>
        </table>
        <div class="pagin">
            <div class="message">共<i class="blue" id="totalCount">1256</i>条记录，当前显示第&nbsp;<i class="blue" id="pageNum">2&nbsp;</i>页
            </div>
            <ul class="paginList" id="page">
                <li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>
                <li class="paginItem"><a href="javascript:;">1</a></li>
                <!--                <li class="paginItem current"><a href="javascript:;">2</a></li>-->
                <li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>
            </ul>
        </div>
    </form>
</div>
<script type="text/javascript">
    $('.tablelist tbody tr:odd').addClass('odd');

    $(function () {
        var firmId = getParameter("firmId");
        queryCProduct(firmId);
    })

    function queryCProduct(firmId, pageNum) {
        $.ajax({
            url: "/purchase/cproduct",
            method: "post",
            dataType: "json",
            data: {firmId: firmId, pageNum: pageNum},
            success: function (data) {
                console.log(data);
                var str = '';
                var da = data.data.data;
                for (var i = 0; i < da.length; i++) {
                    str += `<tr>
                            <td>${i + ((data.data.pageNum - 1) * data.data.pageSize) + 1}</td>
                            <td>${da[i].firmname}</td>
                            <td>${da[i].purchaseId}</td>
                            <td>${da[i].count}</td>
                            <td>￥${da[i].totalMoney}</td>
                            <td>${da[i].purchaseTime}</td>
                            <td>${da[i].uname}</td>
                            <td>
                                <a href="/purchase/purchase/purchaseView?purchaseid=${da[i].purchaseId}" class="tablelink">查看详情</a>
                            </td>
                            </tr>`;
                }
                $("#purchasetr").html(str);
                $("#totalCount").html(data.data.totalCount);
                $("#pageNum").html(data.data.pageNum);
                var begin = data.data.pageNum - 5;
                var end = data.data.pageNum + 4;
                if (begin < 1) {
                    begin = 1;
                    if (data.data.totalPage < 10) {
                        end = data.data.totalPage;
                    } else {
                        end = 10;
                    }
                }
                if (end > data.data.totalPage) {
                    end = data.data.totalPage;
                    if (begin < 0) {
                        begin = 1;
                    }
                }
                var page = '';
                page += '<li class="paginItem" id="up"><a onclick="queryCProduct(' + firmId + ',' + (data.data.pageNum - 1) + ')"><span class="pagepre"></span></a></li>\n';
                for (var j = begin; j <= end; j++) {
                    if (j == data.data.pageNum) {
                        page += '<li class="paginItem current"><a onclick="queryCProduct(' + firmId + ',' + j + ')">' + j + '</a></li>';
                    } else {
                        page += '<li class="paginItem"><a onclick="queryCProduct(' + firmId + ',' + j + ')">' + j + '</a></li>';
                    }
                }
                page += '<li class="paginItem" id="down"><a onclick="queryCProduct(' + firmId + ',' + (data.data.pageNum + 1) + ')"><span class="pagenxt"></span></a></li>';
                $("#page").html(page);

                if (data.data.pageNum == 1) {
                    // $("#up").removeAttr("onclick");
                    $("#up").attr("disabled", true).css("pointer-events", "none")
                }
                if (data.data.pageNum == data.data.totalPage) {
                    // $("#down").removeAttr("onclick");
                    $("#down").attr("disabled", true).css("pointer-events", "none")
                }
            },
            error: function () {
                alert("查询订单失败!");
            }
        })
    }

    function getParameter(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = location.search.substr(1).match(reg);
        if (r != null) {
            return (r[2]);
        }
        return null;
    }

</script>
</body>
</html>
